'use strict';

/* Controllers */
function ModelvalidateCtrl($scope, $http) {
	$scope.setSidebarMenu = function($index){
		$scope.sidebarMenu = $index;
		if ($index == 0){
			$scope.setSidebar2Menu(0);
		}else if ($index == 1){
			$scope.methods2 = [
			{name : "CRA检验", unit : "mm"},
			{name : "MODE检验", unit : null}];
		}
	}
	$scope.setSidebar2Menu = function($index){
		$scope.sidebar2Menu = $index;
		if ($index == 0){
			$scope.methods = ["主观检验", "可信度检验", "Brier评分", "ROC曲线"];
		}else if ($index == 1){
			$scope.methods = ["ME检验", "RMSE检验", "MAE检验", "Bais检验", "ACC检验", "TS评分"
			, "HK检验", "HSS检验", "相关检验", "模糊法检验", "ME检验"];
		}
	}
	$scope.setRightHeaderMenu = function($index){
		$scope.rightHeaderMenu = $index;
	}
	$scope.setSidebarForcastHour = function(hourString){
		$scope.sidebarForcastHour = hourString;
		// set forcast moment
		$scope.forcastMoment = moment($scope.form.sidebarForcastDate);
		$scope.addForcastHour();
		$scope.currentMoment = moment();
		$scope.forcastUnix = $scope.forcastMoment.unix();
		$scope.currentUnix = $scope.currentMoment.unix();
	}
	$scope.isButtonActive = function($index){
		return ($scope.aging1 == $index || $scope.aging2 == $index)
	}
	$scope.setAging = function($index){
		if ($scope.isButtonDisabled($index)){
			return
		}
		if ($scope.aging1 == $index){
			$scope.aging1 = null;
			return
		}
		if ($scope.aging2 == $index){
			$scope.aging2 = null;
			return
		}
		if ($scope.aging1 == null){
			$scope.aging1 = $index;
		}else{
			$scope.aging2 = $index;
		}
	}

	$scope.isButtonDisabled = function($index){
		
		var agingHour = parseInt($scope.agings[$index]);
		var disabled1 = $scope.forcastUnix + agingHour*3600 > $scope.currentUnix;
		var disabled2 = false;
		if ($scope.sidebarMenu == 0 && $scope.sidebarSubMenu[0] != null && $scope.sidebarSubMenu[0] > 1 && $scope.aging1 != null){
			var agingHour1 = parseInt($scope.agings[$scope.aging1]);
			var diffHour = $scope.sidebarHourNumber[$scope.sidebarSubMenu[0]-2]
			disabled2 = Math.abs(agingHour - agingHour1) > diffHour;
		}
		return disabled1 || disabled2;
	}

	$scope.isShowSidebar2tab = function(){
		return $scope.sidebarMenu == 0;
	}
	$scope.isShowAging = function(){
		return ($scope.sidebarMenu == 1 || $scope.sidebar2Menu == 0);
	}
	$scope.isShowDateRange = function(){
		return ($scope.sidebarMenu == 0 && $scope.sidebar2Menu == 1);
	}
	$scope.isShowValidateMethod = function(){
		return $scope.sidebarMenu == 0;
	}
	$scope.isShowValidateMethod2 = function(){
		return $scope.sidebarMenu == 1;
	}

	$scope.addForcastHour = function(){
		if ($scope.sidebarForcastHour == '08h'){
			$scope.forcastMoment.add(8, 'h');
		}else if($scope.sidebarForcastHour == '20h'){
			$scope.forcastMoment.add(20, 'h');
		}
	}
	$scope.$watch('form.sidebarForcastDate', function(newValue, oldValue){
		if (newValue != null){
			// set forcast moment
			$scope.forcastMoment = moment(newValue);
			$scope.addForcastHour();
			$scope.currentMoment = moment();
			$scope.forcastUnix = $scope.forcastMoment.unix();
			$scope.currentUnix = $scope.currentMoment.unix();
		}
	});
	// ----------init options start --------
	// init sidebarMenu
	$scope.sidebarMenus = [
	{
		name : "天气要素",
		children : ["温度", "湿度", "3小时降水", "6小时降水", "12小时降水", "24小时降水"]
	},{
		name : "天气系统",
		children : ["降水系统", "雾霾", "强对流天气"]
	}];
	//init sidebar2 Tab
	$scope.sidebar2Menus = ["时间点检验", "时间段检验"];
	//init models and method
	$scope.models = [{name: "EC", fieldName : "EC"},
	{name: "T639", fieldName : "T639"},
	{name: "NCEP", fieldName : "NCEP"},
	{name: "GRAPES", fieldName : "GRAPES"},
	{name: "日本模式", fieldName : "GapanModel"},
	{name: "德国模式", fieldName : "GermanModel"}];
	$scope.setSidebar2Menu(0);
	//init Date Picker
	$scope.hourOptions =[];
	for (var i = 0; i < 24; i++){
		$scope.hourOptions.push(i + "h");
	}
	$scope.agings = ["003", "006", "009", "012", 
	"015", "018", "021", "024", 
	"027", "030", "033", "036", 
	"039", "042", "045", "048", 
	"051", "054", "057", "060", 
	"063", "066", "069", "072", 
	"078", "084", "090", "096",
	"102", "108", "114", "120",
	"126", "132", "138", "144" ];
	//init sidebar2 Tab
	$scope.rightHeaderMenus = ["江苏省", "扬州市"];
	// ----------init options end --------
	
	//init parameters
	$scope.setMenu(0);
	$scope.form = {};
	$scope.sidebarMenu = 0;
	$scope.sidebarSubMenu = [0, 1];
	// 3小时， 6小时， 12小时， 24小时
	$scope.sidebarHourNumber = [3, 6, 12, 24];
	$scope.sidebar2Menu = 0;
	$scope.sidebarModel = {};
	$scope.models.forEach(function(item){
		$scope.sidebarModel[item.fieldName] = false;
	})
	$scope.sidebarMethod = 0;
	$scope.sidebarMethod2 = {select : 0, unit : 10}

	$scope.form.sidebarForcastDate = "2016-04-07";
	$scope.form.sidebarStartDate = "2016-04-09";
	$scope.form.sidebarEndDate = "2016-04-11";
	$scope.sidebarForcastHour = "08h";
	$scope.form.sidebarStartHour = $scope.hourOptions[8];
	$scope.form.sidebarEndHour = $scope.hourOptions[18];
	$scope.rightHeaderMenu = 0;
}

